<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Grom Hellscream
 * Date: 10/29/12
 * Time: 10:03 AM
 * To change this template use File | Settings | File Templates.
 */
// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.modeladmin');


require_once JPATH_COMPONENT_ADMINISTRATOR.'/helpers/hocham.php';

class VastModelHocham extends JModelAdmin
{
    /**
     * @var		string	The prefix to use with controller messages.
     * @since	1.6
     */
    protected $text_prefix = 'COM_VAST';

    /**
     * Method to test whether a record can be deleted.
     *
     * @param	object	$record	A record object.
     *
     * @return	boolean	True if allowed to delete the record. Defaults to the permission set in the component.
     * @since	1.6
     */
    protected function canDelete($record)
    {
        if (!empty($record->id)) {
            $user = JFactory::getUser();
            return $user->authorise('core.delete', 'com_vast.hocham.'.(int) $record->id);
        }
    }

	/**
	 * Method to save the form data.
	 *
	 * @param   array  $data  The form data.
	 *
	 * @return  boolean  True on success, False on error.
	 *
	 * @since   11.1
	 */
	public function save($data)
    {        
        $result = parent::save($data);

        if ($result)
        {
            JVastLog::addLog(date('Y-m-d'), $_SERVER['REMOTE_ADDR'], 'Cập nhật thông tin học hàm ' . $data['ten'], JFactory::getUser()->id, VastHelper::getHosocanbo()->id);            
        }
        
        return $result;
    }    
    
	/**
	 * Method to delete one or more records.
	 *
	 * @param   array  &$pks  An array of record primary keys.
	 *
	 * @return  boolean  True if successful, false if an error occurs.
	 *
	 * @since   11.1
	 */
	public function delete(&$pks)
	{
        $result = parent::delete($pks);
        if ($result)
        {
            $db = $this->getDbo();
            $query = $db->getQuery(true);  
            
            JVastLog::addLog(date('Y-m-d'), $_SERVER['REMOTE_ADDR'], 'Xóa thông tin của học hàm', JFactory::getUser()->id, VastHelper::getHosocanbo()->id);            
        }
        
        return $result;
    }    
    
    /**
     * Returns a Table object, always creating it.
     *
     * @param	type	The table type to instantiate
     * @param	string	A prefix for the table class name. Optional.
     * @param	array	Configuration array for model. Optional.
     *
     * @return	JTable	A database object
     */
    public function getTable($type = 'Hocham', $prefix = 'JTable', $config = array())
    {
        return JTable::getInstance($type, $prefix, $config);
    }

    /**
     * Method to get the record form.
     *
     * @param	array	$data		Data for the form.
     * @param	boolean	$loadData	True if the form is to load its own data (default case), false if not.
     *
     * @return	mixed	A JForm object on success, false on failure
     * @since	1.6
     */
    public function getForm($data = array(), $loadData = true)
    {
        // Get the form.
        $form = $this->loadForm('com_vast.hocham', 'hocham', array('control' => 'jform', 'load_data' => $loadData));
        if (empty($form)) {
            return false;
        }
        $jinput = JFactory::getApplication()->input;

        // The front end calls this model and uses a_id to avoid id clashes so we need to check for that first.
        if ($jinput->get('a_id'))
        {
            $id =  $jinput->get('a_id', 0);
        }
        // The back end uses id so we use that the rest of the time and set it to 0 by default.
        else
        {
            $id =  $jinput->get('id', 0);
        }
        // Determine correct permissions to check.
        if ($this->getState('hocham.id'))
        {
            $id = $this->getState('hocham.id');
            // Existing record. Can only edit in selected categories.
            $form->setFieldAttribute('catid', 'action', 'core.edit');
            // Existing record. Can only edit own articles in selected categories.
            $form->setFieldAttribute('catid', 'action', 'core.edit.own');
        }
        else
        {
            // New record. Can only create in selected categories.
            $form->setFieldAttribute('catid', 'action', 'core.create');
        }

        $user = JFactory::getUser();

        return $form;
    }

    /**
     * Method to get the data that should be injected in the form.
     *
     * @return	mixed	The data for the form.
     * @since	1.6
     */
    protected function loadFormData()
    {
        // Check the session for previously entered form data.
        $data = JFactory::getApplication()->getUserState('com_vast.edit.hocham.data', array());

        if (empty($data)) {
            $data = $this->getItem();

            // Prime some default values.
            if ($this->getState('hocham.id') == 0) {
                $app = JFactory::getApplication();
                $data->set('catid', JRequest::getInt('catid', $app->getUserState('com_vast.hochams.filter.category_id')));
            }
        }

        return $data;
    }
}